package ui.control
{
	import flash.display.BitmapData;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	
	import loaderData.LoaderSys;
	
	import texture.BitmapEx;
	
	import ui.GuiWnd;

	/**
	 * 静态控件 
	 * @author weiping
	 * 
	 */
	public class GuiStatic extends GuiWnd
	{
		protected var textField:TextField;
		protected var img:BitmapEx;
		
		public function GuiStatic()
		{
			textField = new TextField();
			textField.x = 0;
			textField.y = 0;
			isSelectable = false;
			textField.multiline = true;
			textField.wordWrap = true;
			textField.autoSize = TextFieldAutoSize.LEFT;
		}
		
		override public function init(xml:XML):void
		{
			if(xml.@text)
				text = xml.@text;
			
			if(xml.@x)
			{
				this.x = xml.@x;
			}
			
			if(xml.@y)
			{
				this.y = xml.@y;
			}
			
			if(xml.@width)
			{
				this.nWidth = xml.@w;
			}
			
			if(xml.@height)
			{
				this.nHeight = xml.@height;
			}
			
			if(xml.@text_x)
			{
				setTextPos(xml.@text_x, textField.y);
			}
			
			if(xml.@text_y)
			{
				setTextPos(textField.x, xml.@text_y);
			}
			
			if(xml.@text_w)
			{
				setTextSize(xml.@text_w, textField.height);
			}
			
			if(xml.@text_h)
			{
				setTextSize(textField.width, xml.@text_h);
			}	
			
			if(xml.@input)
			{
				this.input = xml.@input;
			}
			
			if(xml.@img)
			{
				var tex:BitmapData = LoaderSys.inst.getBitmapDataByClass(xml.@img);
				if(tex) backImg = tex;
			}
		}
		
		public function drawBack(cor:uint, alpha:Number = 1):void
		{
			this.graphics.clear();
			this.graphics.beginFill(cor, alpha);
			this.graphics.drawRect(0,0,this.width, this.height);
			this.graphics.endFill();
		}
		
		public function showText(bShow:Boolean):void
		{
			if(bShow && !this.contains(textField))
				this.addChild(textField);
			else if(!bShow && this.contains(textField))
				this.removeChild(textField);
		}
		
		/**
		 * 是否输入文本框 
		 * @param b
		 * 
		 */		
		public function set input(b:Boolean):void
		{
			textField.type = b ? TextFieldType.INPUT : TextFieldType.DYNAMIC;
			
			isSelectable = b;
			
			showText(true);
		}
		
		/**
		 * 是否多行 
		 * @param b
		 * 
		 */		
		public function setMultiline(b:Boolean):void
		{
			textField.multiline = b;
			textField.wordWrap = b;
		}
		
		/**
		 * 设置文本宽高 
		 * @param w
		 * @param h
		 * 
		 */		
		public function setTextSize(w, h):void
		{
			textField.width 	= w;
			textField.height 	= h;
		}
		
		/**
		 *设置文本HTML文本 
		 * @param str
		 * 
		 */		
		public function set text(str:String):void
		{
			textField.htmlText = str;
			
			if(textField.text != "")
				showText(true);
			else
				showText(false);
		}
		
		public function get text():String
		{
			return textField.text;
		}
		
		/**
		 * 设置文本基本颜色 
		 * @param cor
		 * 
		 */		
		public function setTextCor(cor:uint):void
		{
			textField.textColor = cor;
		}
		
		/**
		 *设置文本坐标 
		 * @param nX
		 * @param nY
		 * 
		 */		
		public function setTextPos(nX:int, nY:int):void
		{
			this.textField.x = nX;
			this.textField.y =  nY;
		}

		override public function set nWidth(value:int):void
		{
			super.nWidth = value;
			
			if(img)
			{
				img.width = value;
			}
		}
		
		override public function set nHeight(value:int):void
		{
			super.nWidth = value;
			
			if(img)
			{
				img.height = value;
			}
		}
		
		/**
		 * 是否可以选中 
		 * @param value
		 * 
		 */		
		public function set isSelectable(value:Boolean):void
		{
			this.textField.selectable = value;
		}

		/**
		 * 设置背景 
		 * @param value
		 * 
		 */		
		public function set backImg(value:BitmapData):void
		{
			if(img)
			{
				img.updataBitmapData(value);	
			}
			else
			{
				img = new BitmapEx(value);
				this.addChildAt(img, 0);
			}
			
			//限定大小
			img.width = nWidth;
			img.height = nWidth;
				
		}

	}
}